/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package janelas;

import Classes.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniela
 */
public class JanelaIndicar extends javax.swing.JFrame {

    private JanelaLivro janelaAnterior;
    private Item item;
    private Sistema sistema;
    
    public JanelaIndicar(JanelaLivro janelaAnterior, Item item) {
        initComponents();
        
       setResizable(false); 
       setLocationRelativeTo(null);
       this.janelaAnterior = janelaAnterior;
       this.item = item;
       sistema = janelaAnterior.getSistema();
       preencheListaUsuarios();
       informacoesDeIndicacao();
    }
    
    public Sistema getSistema(){
        return janelaAnterior.getSistema();
    }
    
    public void informacoesDeIndicacao(){
       campoLivro.setText(item.getNome());
       campoRemetente.setText(sistema.getUserName());
    }

    private void preencheListaUsuarios() {
        final List<String> listaDeUsuarios = new ArrayList<>();
        for (Usuario u : janelaAnterior.getSistema().getLista()) {
            if (!u.getLogin().equals(janelaAnterior.getSistema().getUserName())) {
                listaDeUsuarios.add(u.getLogin());
            }
        }
        preencheUsuarios(listaDeUsuarios);

    }

    public void preencheUsuarios(final List<String> nomes) {
        listaDeUsuarios.setModel(new AbstractListModel() {
            public int getSize() {
                return nomes.size();
            }

            public Object getElementAt(int i) {
                return nomes.get(i);
            }
        });
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoRemetente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaDeUsuarios = new javax.swing.JList();
        bEnviar = new javax.swing.JButton();
        campoLivro = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        bCancelar = new javax.swing.JButton();
        campoPesquisar = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        campoComentario = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Indicar livro");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        jLabel1.setText("Remetente");

        campoRemetente.setText("PEGAR getUserName()");
        campoRemetente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoRemetenteActionPerformed(evt);
            }
        });

        jLabel2.setText("Destinatários");

        jLabel4.setFont(new java.awt.Font("Franklin Gothic Medium Cond", 1, 18)); // NOI18N
        jLabel4.setText("Você está indicando um livro");

        jLabel5.setText("Livro");

        jScrollPane1.setViewportView(listaDeUsuarios);

        bEnviar.setText("Enviar Indicação");
        bEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEnviarActionPerformed(evt);
            }
        });

        campoLivro.setText("PEGAR SÓ O getTitulo()");
        campoLivro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoLivroActionPerformed(evt);
            }
        });

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        campoPesquisar.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                campoPesquisarCaretUpdate(evt);
            }
        });
        campoPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoPesquisarActionPerformed(evt);
            }
        });

        campoComentario.setColumns(20);
        campoComentario.setLineWrap(true);
        campoComentario.setRows(5);
        campoComentario.setWrapStyleWord(true);
        jScrollPane3.setViewportView(campoComentario);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campoLivro)
                            .addComponent(campoRemetente))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 20, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(bCancelar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bEnviar))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(20, 20, 20))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3))
                            .addComponent(jLabel2))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(campoLivro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoRemetente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(campoPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bEnviar)
                    .addComponent(bCancelar))
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEnviarActionPerformed
        String login = (String) listaDeUsuarios.getSelectedValue();
        if (login != null) {
            Usuario usuarioSelecionado = null;
            for (Usuario u : janelaAnterior.getSistema().getLista()) {
                if (u.getLogin().equals(login)) {
                    usuarioSelecionado = u;
                }
            }
            Classes.Indicacao indicacao = new Classes.Indicacao(janelaAnterior.getSistema().getUsuarioAtivo(), item, campoComentario.getText());

            usuarioSelecionado.addIndicacao(indicacao);
            JOptionPane.showMessageDialog(this, "Indicação feita com sucesso!");
            this.dispose();
            janelaAnterior.setEnabled(true);
            janelaAnterior.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um usuário para indicar!");
        }
        
    }//GEN-LAST:event_bEnviarActionPerformed

    private void campoRemetenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoRemetenteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoRemetenteActionPerformed

    private void campoPesquisarCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_campoPesquisarCaretUpdate
        String chave = campoPesquisar.getText();
        final List<String> listaDeUsuarios = new ArrayList<>();
        for (Usuario u : janelaAnterior.getSistema().getLista()) {
            if (u.getLogin().toLowerCase().contains(chave.toLowerCase()) && !u.getLogin().equals(janelaAnterior.getSistema().getUserName())) {
                listaDeUsuarios.add(u.getLogin());
            }
        }
        preencheUsuarios(listaDeUsuarios);
    }//GEN-LAST:event_campoPesquisarCaretUpdate

    private void campoLivroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoLivroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoLivroActionPerformed

    private void campoPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoPesquisarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoPesquisarActionPerformed

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
        janelaAnterior.setVisible(true);
        janelaAnterior.setEnabled(true);
    }//GEN-LAST:event_bCancelarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        janelaAnterior.setVisible(true);
        janelaAnterior.setEnabled(true);
    }//GEN-LAST:event_formWindowClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton bEnviar;
    private javax.swing.JTextArea campoComentario;
    private javax.swing.JTextField campoLivro;
    private javax.swing.JTextField campoPesquisar;
    private javax.swing.JTextField campoRemetente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList listaDeUsuarios;
    // End of variables declaration//GEN-END:variables
}
